﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinQ_DTO;
using BUS;
using System.Xml;

namespace TemplateH4
{
    public partial class ThanhToan : System.Web.UI.Page
    {
        double TongCong = 0;
        protected void Page_PreInit(object sender, EventArgs e)
        {
            String thm;
            thm = (string)Session["themeSelected"];
            if (thm != null)
            {
                Page.Theme = thm;
            }
            else
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("~/config.xml"));
                XmlNode root = doc.DocumentElement;
                String theme = root.SelectSingleNode("theme").ChildNodes[0].Value.Trim();
                Session.Add("themeSelected", theme);
                Page.Theme = theme;
            }

        }



        protected void Page_Load(object sender, EventArgs e)
        {
            this.lbThongBao.Visible = false;

                List<shop_sanpham> GioHang = (List<shop_sanpham>)Session["GioHang"];
                if (GioHang != null)
                {
                    TongCong = this.TinhTien(GioHang) - this.TinhTien(GioHang) * GetCutDownRateFromPoint();
                    this.lbTongCong.Text = TongCong + " VND";
                }
                else
                {
                    TongCong = 0;
                    this.lbTongCong.Text = "0 VNĐ";
                }

            

        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {

        }

        protected void btnGioHang_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/GioHang.aspx");
        }

        protected void btnThanhToan_Click(object sender, EventArgs e)
        {
            if (!KiemTraNhapLieu())
            {
                lbThongBao.Text = "Vui lòng nhập đầy đủ thông tin ở trên";
                lbThongBao.Visible = true;
            }

            LuuHoaDon();
            Response.Redirect("~/ThanhToanThanhCong.aspx");

            //string IDCardTarget = "1234567890123490";
            //string IDCardSource = this.txtCardID.Text;
            //string IDCardSourceCode = this.txtCode.Text;
            //BankingServices.Service1Client services = new TemplateH4.BankingServices.Service1Client();
            
            //int result = -13;
            //if (cmbLoaiTT.SelectedValue == "VND")
            //    result = services.ExChangeVND(IDCardSource, IDCardSourceCode, IDCardTarget, (decimal)TongCong);
            //if (cmbLoaiTT.SelectedValue == "USD")
            //    result = services.ExChangeUSD(IDCardSource, IDCardSourceCode, IDCardTarget, (decimal)TongCong);


            //switch (result)
            //{
            //    case 1:
            //        if (TongCong != 0)
            //            LuuHoaDon();
            //        Response.Redirect("~/ThanhToanThanhCong.aspx");
            //        break;
            //    case 0:
            //        lbThongBao.Visible = true;
            //        lbThongBao.Text = "The number of money is not match";
            //        break;
            //    case -1:
            //        lbThongBao.Visible = true;
            //        lbThongBao.Text = "ID of source card do not exist";
            //        break;
            //    case -2:
            //        lbThongBao.Visible = true;
            //        lbThongBao.Text = "Source card is not enought money";
            //        break;
            //    case -3:
            //        lbThongBao.Visible = true;
            //        lbThongBao.Text = "Security code do not match";
            //        break;
            //    case -4:
            //        lbThongBao.Visible = true;
            //        lbThongBao.Text = "ID of target card do not match";
            //        break;
            //} 

        }

        private bool KiemTraNhapLieu()
        {
            if (this.txtCardID.Text == "")
                return false;
            if (txtCode.Text == "")
                return false;
            if (txtDiaChi.Text == "")
                return false;
            return true;
        }

        //Tong tien tra (chua co tinh diem tich luy)
        private double TinhTien(List<shop_sanpham> listSP)
        {
            double ThanhTien = 0;
            foreach (shop_sanpham sp in listSP)
            {
                ThanhTien += (double)sp.SoLuongNhap * TinhTienSanPham(sp);
            }

            return ThanhTien;

        }

        private double TinhTienSanPham(shop_sanpham sp)
        {
            double CutdownRate = 0;

            foreach (shop_sanpham_khuyenmai spkm in sp.shop_sanpham_khuyenmais)
                if (spkm.shop_khuyenmai.MaKhuyenMai == 1)                //la giam gia
                    CutdownRate += (float)spkm.shop_khuyenmai.MucKhuyenMai / 100;

            return (double)sp.DonGia - (double)sp.DonGia * CutdownRate;
        }

        private double GetCutDownRateFromPoint()
        {
            if ((Session["IsLogin"] == null) || ((int)Session["IsLogin"] == 0))
            {
                return 0;
            }
            UserBUS UsrBUS = new UserBUS();
            string UserName = (string)Session["UserName"];
            aspnet_User User = UsrBUS.LayUserTheoTen(UserName);
            int DiemTL = (int)User.aspnet_Membership.DiemTichLuy;

            CauHinhBUS chBus = new CauHinhBUS();
            List<decimal> ProRates = chBus.LayDSGiaTriKhuyenMai();
            List<int> ProPoints = chBus.LayDSDieuKienKhuyenMai();

            for (int i = 2; i >= 0; i--)
                if (DiemTL >= ProPoints[i])
                    return (double)ProRates[i];

            return 0;

        }

        public void LuuHoaDon()
        {
            
            HoaDonBUS hdBus = new HoaDonBUS();
            ChiTietHoaDonBUS cthdBus = new ChiTietHoaDonBUS();
            shop_hoadon hd = new shop_hoadon();
            shop_chitiethoadon cthd;
            List<shop_sanpham> GioHang = (List<shop_sanpham>)Session["GioHang"];

            if ((Session["IsLogin"]!=null)&&(int)Session["IsLogin"] == 1)
            {
                UserBUS UsrBUS = new UserBUS();
                string UserName = (string)Session["UserName"];
                aspnet_User User = UsrBUS.LayUserTheoTen(UserName);
                hd.UserId = User.UserId;
            }

            
            hd.NgayLap = DateTime.Now;
            hd.DiaChiGiaoHang = this.txtDiaChi.Text;
            hd.ThanhTien = (decimal)TongCong;
            int id = hdBus.ThemHoaDon_LayID(hd);
            id = hd.MaHoaDon;

            foreach (shop_sanpham sp in GioHang)
            {
                cthd = new shop_chitiethoadon();
                cthd.DonGia = sp.DonGia;
                cthd.MaHoaDon = id;
                cthd.MaSanPham = sp.GhiChu + sp.MaSanPham.ToString();
                cthd.SoLuong = (long)sp.SoLuongNhap;
                cthd.TenSanPham = sp.TenSanPham;
                cthdBus.ThemChiTietHoaDon(cthd);
            }

        }

    }
}
